﻿using Microsoft.Extensions.DependencyInjection;
using StartsEAS_API.BLL.Interfaces.IResultB;
using StartsEAS_API.DAL.ResultsD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.ResultBLL
{
    public class ResultBLL : IResultBLL
    {
        private readonly StarsEASContext _context;
        public ResultsDAL _resultDal;
        public ResultBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _resultDal = new(_context);
        }

        public ResultUtils GetAllResults()
        {
            return _resultDal.GetAllResults();
        }

        public ResultUtils GetResultsByClassId(string token, string classId)
        {
            try
            {
                IEnumerable<Object> results = _resultDal.GetResultsByClassId(token, classId);
                if (results == null)
                    return new ResultUtils()
                    {
                        code = 500,
                        message = "数据为空"
                    };
                return new ResultUtils()
                {
                    code = 200,
                    message = results
                };
            }
            catch (Exception e)
            {
                return new ResultUtils()
                {
                    code = 500,
                    message = e.Message
                };
            }
        }

        public ResultUtils GetResultsById(int id)
        {
            Result result = _resultDal.GetResultsById(id);
            if (result == null)
                return new ResultUtils() {code=500,message="查无该成绩信息" };
            return new ResultUtils() { code = 200, message = result };
        }

        public ResultUtils GetResultsByToken(string token)
        {
            try
            {
                IEnumerable<Object> results = _resultDal.GetResultsByToken(token);
                if(results==null)
                    return new ResultUtils()
                    {
                        code = 500,
                        message = "数据为空"
                    };
                return new ResultUtils()
                {
                    code = 200,
                    message = results
                };
            }
            catch (Exception e)
            {
                return new ResultUtils()
                {
                    code = 500,
                    message = e.Message
                };
            }
        }
    }
}
